package com.ruoyi.pingan.fymodule.goodsinfo.goods.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.pingan.annotation.CommonLog;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.entity.SxoGoods;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.param.SxoGoodsAddParam;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.param.SxoGoodsEditParam;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.param.SxoGoodsIdParam;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.param.SxoGoodsPageParam;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.service.SxoGoodsService;
import com.ruoyi.pingan.fymodule.thirdpa.util.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 商品信息控制器
 *
 * @author wangy
 * @date  2025/07/25 11:54
 */
@Tag(name = "商品信息控制器")
@RestController
@Validated
public class SxoGoodsController {

    @Resource
    private SxoGoodsService sxoGoodsService;

    /**
     * 获取商品信息分页
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "获取商品信息分页")
    @SaCheckPermission("/goods/page")
    @PostMapping("/goods/page")
    public CommonResult<Page<SxoGoods>> page(@RequestBody SxoGoodsPageParam sxoGoodsPageParam) {
        return CommonResult.data(sxoGoodsService.page(sxoGoodsPageParam));
    }

    /**
     * 添加商品信息
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "添加商品信息")
    @CommonLog("添加商品信息")
    @SaCheckPermission("/goods/add")
    @PostMapping("/goods/add")
    public CommonResult<String> add(@RequestBody @Valid SxoGoodsAddParam sxoGoodsAddParam) {
        sxoGoodsService.add(sxoGoodsAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑商品信息
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "编辑商品信息")
    @CommonLog("编辑商品信息")
    @SaCheckPermission("/goods/edit")
    @PostMapping("/goods/edit")
    public CommonResult<String> edit(@RequestBody @Valid SxoGoodsEditParam sxoGoodsEditParam) {
        sxoGoodsService.edit(sxoGoodsEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除商品信息
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "删除商品信息")
    @CommonLog("删除商品信息")
    @SaCheckPermission("/goods/delete")
    @PostMapping("/goods/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<SxoGoodsIdParam> sxoGoodsIdParamList) {
        sxoGoodsService.delete(sxoGoodsIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取商品信息详情
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "获取商品信息详情")
    @SaCheckPermission("/goods/detail")
    @GetMapping("/goods/detail")
    public CommonResult<SxoGoods> detail(@Valid SxoGoodsIdParam sxoGoodsIdParam) {
        return CommonResult.data(sxoGoodsService.detail(sxoGoodsIdParam));
    }

    /**
     * 下载商品信息导入模板
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "下载商品信息导入模板")
    @GetMapping(value = "/goods/downloadImportTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        sxoGoodsService.downloadImportTemplate(response);
    }

    /**
     * 导入商品信息
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "导入商品信息")
    @CommonLog("导入商品信息")
    @SaCheckPermission("/goods/importData")
    @PostMapping("/goods/importData")
    public CommonResult<JSONObject> importData(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(sxoGoodsService.importData(file));
    }

    /**
     * 导出商品信息
     *
     * @author wangy
     * @date  2025/07/25 11:54
     */
    @Operation(summary = "导出商品信息")
    @SaCheckPermission("/goods/exportData")
    @PostMapping(value = "/goods/exportData", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportData(@RequestBody List<SxoGoodsIdParam> sxoGoodsIdParamList, HttpServletResponse response) throws IOException {
        sxoGoodsService.exportData(sxoGoodsIdParamList, response);
    }
}
